package cn.zqh.auth.service.impl;

import cn.zqh.auth.config.BSException;
import cn.zqh.auth.domain.UserDTO;
import cn.zqh.auth.holder.LoginUserHolder;
import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.api.ResultCode;
import cn.zqh.uuid.utils.SnowflakeId;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zqh.auth.mapper.UserMapper;
import cn.zqh.auth.pojo.User;
import cn.zqh.auth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.security.auth.login.LoginContext;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-12-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private LoginUserHolder loginUserHolder;

    @Autowired
    private PasswordEncoder passwordEncoder;






    /**
      * @Author: zqh
      * @Date:
      * @Description: 注册一个新用户
    */
    @Override
    public CommonResult createANewUser(String qq, String password) {
        if (qq == null || password == null) {
           throw new BSException(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMessage());
        }

        User user = new User()
                .setUserId("user:" + SnowflakeId.nextId())
                .setNickName("新用户"+SnowflakeId.nextId())
                .setQqEmail(qq)
                .setPassword(passwordEncoder.encode(password))
                //性别默认为男
                .setSex(1)
                //头像默认基础头像，后面添加
                .setAvatar("http://sddklslkjsd")
                .setIntro("我是一名新用户")
                //地址默认为空
                .setAddress("**********")
                //是否拥有店铺
                .setIsStore(0)
                .setHavaMoney(0.0)
                .setIsDelete(0);
        int insert = userMapper.insert(user);
        if (insert != 1){
            throw new BSException(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
        }
        return CommonResult.success(ResultCode.SUCCESS);
    }
}
